package com.android.tnovoselec.travelassistant.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.tnovoselec.travelassistant.R;
import com.android.tnovoselec.travelassistant.Util;
import com.android.tnovoselec.travelassistant.activity.BankomatiMapActivity;
import com.android.tnovoselec.travelassistant.activity.BenzinskeMapActivity;
import com.android.tnovoselec.travelassistant.activity.HomeActivity;
import com.android.tnovoselec.travelassistant.activity.MeteoActivity;
import com.android.tnovoselec.travelassistant.activity.HomeActivity.MeteoReadyListener;
import com.android.tnovoselec.travelassistant.dto.MeteoFavoritesDto;
import com.android.tnovoselec.travelassistant.dto.MeteoResultHolder;
import com.google.android.maps.GeoPoint;
import com.nth.android.image.ImageDownloader;
import com.nth.android.lbs.location.MyLocationService;

public class HomeFragment extends Fragment implements OnClickListener, MeteoReadyListener {

	private ImageButton bankomatiButton;
	private ImageButton benzinskeButton;
	private ImageButton meteoButton;
	private ImageButton settingsButton;

	private ImageView currentMeteoIcon;
	private TextView currentCityName;
	private TextView currentCityTemp;

	private View currentMeteoContainer;

	private Animation animShow;
	private Animation animHide;

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		if (activity instanceof HomeActivity) {
			((HomeActivity) activity).setListener(this);
		}
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		animShow = AnimationUtils.loadAnimation(getActivity(), R.anim.appear);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.fragment_dashboard, container, false);
		bankomatiButton = (ImageButton) v.findViewById(R.id.home_btn_bankomati);
		benzinskeButton = (ImageButton) v.findViewById(R.id.home_btn_benzinske);
		settingsButton = (ImageButton) v.findViewById(R.id.home_btn_settings);
		meteoButton = (ImageButton) v.findViewById(R.id.home_btn_meteo);

		currentCityName = (TextView) v.findViewById(R.id.home_current_weather_city_name);
		currentCityTemp = (TextView) v.findViewById(R.id.home_current_weather_temp);
		currentMeteoIcon = (ImageView) v.findViewById(R.id.home_current_weather_icon);
		currentMeteoContainer = v.findViewById(R.id.home_current_weather_container);

		bankomatiButton.setOnClickListener(this);
		benzinskeButton.setOnClickListener(this);
		settingsButton.setOnClickListener(this);
		meteoButton.setOnClickListener(this);
		return v;
	}

	@Override
	public void onClick(View v) {
		if (v == bankomatiButton) {
			getActivity().startActivity(new Intent(getActivity(), BankomatiMapActivity.class));
		} else if (v == benzinskeButton) {
			getActivity().startActivity(new Intent(getActivity(), BenzinskeMapActivity.class));
		} else if (v == meteoButton) {
			getActivity().startActivity(new Intent(getActivity(), MeteoActivity.class));
		}

	}

	@Override
	public void onMeteoReady() {
		Log.i("MeteoReady", "");
		if (currentMeteoContainer != null) {
			Log.i("MeteoReady", "1");
			GeoPoint myLocation = MyLocationService.getLastKnownGeoPoint(getActivity());
			MeteoFavoritesDto currentMeteo = Util.getNearestCityMeteoData(myLocation, MeteoResultHolder.getAll());
			if (currentMeteo != null) {
				Log.i("MeteoReady", "2");
				currentMeteoContainer.setVisibility(View.VISIBLE);
				currentMeteoContainer.startAnimation(animShow);
				if (currentCityName != null) {
					currentCityName.setText(currentMeteo.getTitle());
				}
				if (currentCityTemp != null) {
					currentCityTemp.setText(String.format(getString(R.string.current_temperature), currentMeteo.getTemperature()));
				}
				if (currentMeteoIcon != null) {
					ImageDownloader.getInstance().download(currentMeteo.getIconUrl(), currentMeteoIcon);
				}
			}
		}

	}

}
